package com.ruoyi.project.tickets.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.common.utils.CommonUtil;
import com.ruoyi.project.tickets.domain.constant.TicketsConstant;
import com.ruoyi.project.tickets.domain.vo.SerialVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.tickets.mapper.SerialMapper;
import com.ruoyi.project.tickets.domain.Serial;
import com.ruoyi.project.tickets.service.ISerialService;

/**
 * 核销表Service业务层处理
 *
 * @author 孙山伟
 * @date 2023-07-01
 */
@Service
public class SerialServiceImpl implements ISerialService
{
    @Autowired
    private SerialMapper serialMapper;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询核销表
     *
     * @param id 核销表ID
     * @return 核销表
     */
    @Override
    public Serial selectSerialById(Long id)
    {
        return serialMapper.selectSerialById(id);
    }

    /**
     * 查询核销表列表
     *
     * @param serial 核销表
     * @return 核销表
     */
    @Override
    public List<SerialVo> selectSerialList(Serial serial)
    {
        return serialMapper.selectSerialList(serial);
    }

    /**
     * 新增核销表
     *
     * @param serial 核销表
     * @return 结果
     */
    @Override
    public int insertSerial(Serial serial)
    {
        serial.setCreateTime(DateUtils.getNowDate());
        return serialMapper.insertSerial(serial);
    }

    /**
     * 修改核销表
     *
     * @param serial 核销表
     * @return 结果
     */
    @Override
    public int updateSerial(Serial serial)
    {
        serial.setUpdateTime(DateUtils.getNowDate());
        return serialMapper.updateSerial(serial);
    }

    /**
     * 批量删除核销表
     *
     * @param ids 需要删除的核销表ID
     * @return 结果
     */
    @Override
    public int deleteSerialByIds(Long[] ids)
    {
        return serialMapper.deleteSerialByIds(ids);
    }

    /**
     * 删除核销表信息
     *
     * @param id 核销表ID
     * @return 结果
     */
    @Override
    public int deleteSerialById(Long id)
    {
        return serialMapper.deleteSerialById(id);
    }

    @Override
    public SerialVo selectBySerialNumber(String serialNumber) {
        return serialMapper.selectBySerialNumber(serialNumber);
    }

    @Override
    public int goOff(Long id) {
        LoginUser user = tokenService.getLoginUser(ServletUtils.getRequest());
        Serial serial=new Serial();
        serial.setId(id);
        serial.setIsOff(TicketsConstant.IS_OFF);
        serial.setOffTime(DateUtils.getNowDate());
        serial.setOffUserId(user.getUser().getUserId());
        return serialMapper.updateSerial(serial);
    }

    @Override
    public void rePrintList(Long[] ids) {
        LoginUser user = tokenService.getLoginUser(ServletUtils.getRequest());
        for(Long id :ids){
            Serial   serial=serialMapper.selectSerialById(id);
            if(TicketsConstant.IS_OFF.equals(serial.getIsOff())){
                continue;
            }
            serial.setSerialNumber(CommonUtil.genOrderNum(user.getUser().getUserId()));
            serial.setCode(CommonUtil.getCode(user.getUser().getUserId().toString()));
            serialMapper.updateSerial(serial);
        }
    }
}
